﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NK.Mvc3.Models;
using NK.Mvc3.DBL;

namespace NK.Mvc3.Controllers
{ 
    public class StatisticsController : Controller
    {
        private StoreContext db = new StoreContext();

        //
        // GET: /Statistics/

        public ViewResult Index()
        {
            var statistics = db.Statistics.Include(s => s.ProductInfo);
            return View(statistics.ToList());
        }

        //
        // GET: /Statistics/Details/5

        public ViewResult Details(int id)
        {
            Statistics statistics = db.Statistics.Find(id);
            return View(statistics);
        }

        //
        // GET: /Statistics/Create

        public ActionResult Create()
        {
            ViewBag.ProductInfoID = new SelectList(db.ProductInfos, "ID", "Group");
            return View();
        } 

        //
        // POST: /Statistics/Create

        [HttpPost]
        public ActionResult Create(Statistics statistics)
        {
            if (ModelState.IsValid)
            {
                db.Statistics.Add(statistics);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.ProductInfoID = new SelectList(db.ProductInfos, "ID", "Group", statistics.ProductInfoID);
            return View(statistics);
        }
        
        //
        // GET: /Statistics/Edit/5
 
        public ActionResult Edit(int id)
        {
            Statistics statistics = db.Statistics.Find(id);
            ViewBag.ProductInfoID = new SelectList(db.ProductInfos, "ID", "Group", statistics.ProductInfoID);
            return View(statistics);
        }

        //
        // POST: /Statistics/Edit/5

        [HttpPost]
        public ActionResult Edit(Statistics statistics)
        {
            if (ModelState.IsValid)
            {
                db.Entry(statistics).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ProductInfoID = new SelectList(db.ProductInfos, "ID", "Group", statistics.ProductInfoID);
            return View(statistics);
        }

        //
        // GET: /Statistics/Delete/5
 
        public ActionResult Delete(int id)
        {
            Statistics statistics = db.Statistics.Find(id);
            return View(statistics);
        }

        //
        // POST: /Statistics/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Statistics statistics = db.Statistics.Find(id);
            db.Statistics.Remove(statistics);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}